// pages/index/cardCenter/cardCenter.js
var app = getApp(),
  that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardCenterDetail: {},
    showModal: !1,
    bizUserInfo: {},
    kqyue: !0,
    balance: '',
    amt: 0.0,
    num:0
  },
  onChangeShowState: function() {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  onLoad: function(opt) {
    that = this
    console.log(opt)
    this.data.id = opt.id;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    app.getData({
      url: app.api.getCardCenterDetail(that.data.id),
      method: 'GET',
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            that.setData({
              cardCenterDetail: res.data.data,
              amt: res.data.data.amt,
              num: res.data.data.num,
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        }
      }
    });
    // 获取余额
    var bizUserInfo = wx.getStorageSync('bizUserInfo')
    console.log(bizUserInfo.balance)
    this.setData({
      balance: bizUserInfo.balance,
      bizUserInfo: bizUserInfo
    })
  },
  radioChange: function(t) {
    console.log(t)
    console.log("radio1发生change事件，携带value值为：", t.detail.value), "wxzf" == t.detail.value && this.setData({
      zffs: 1,
      zfwz: "微信支付",
      btntype: "btn_ok1",
    }), "yezf" == t.detail.value && this.setData({
      zffs: 2,
      zfwz: "余额支付",
      btntype: "btn_ok2",
    });
  },
  qrmd: function() {
    // console.log(123)
    this.setData({
      showModal: !0
    })
  },
  yczz: function() {
    this.setData({
      showModal: !1
    })
  },
  formSubmit: function(t) {
    console.log(t.detail.value.radiogroup)
    if (t.detail.value.radiogroup) {
      if (t.detail.value.radiogroup == "wxzf") {
        that = this
        // 提交订单
        that.pay(1, t.detail.value.radiogroup)
      }
      if (t.detail.value.radiogroup == "yezf") {
        var z = wx.getStorageSync('bizUserInfo').balance,
          D = Number(this.data.amt);
          console.log(this.data.amt)
        if (console.log(z, D), z < D) return void wx.showToast({
          title: "余额不足支付",
          icon: "loading"
        });
        that.pay(2, t.detail.value.radiogroup)
      }
    } else {
      wx.showToast({
        title: '请先选择支付方式！',
        icon: 'none'
      })
    }

  },
  pay: function(payType, type) {
    //添加订单
    app.getData({
      url: app.api.addOrder(),
      method: "POST",
      data: {
        companyId: wx.getStorageSync('companyId'),
        amt: that.data.amt,
        cardId: that.data.id,
        memberId: wx.getStorageSync('bizUserInfo').id,
        num: that.data.num,
        payType:payType,
        
      },
      ok: function(res) {
        console.log(res)
        //  添加订单成功之后去支付
        console.log('***'+res.data.data.id)
        if (type == "wxzf") {
          that.wxPay(res.data.data.id)
        }
        if (type == "yezf") {
          wx.navigateTo({
            url: '../../../../mine/pages/wdck/wdck'
          });
          that.getBizUserInfo()
        }
        that.setData({
          showModal: !1
        })
      }
      
    })
  },
  getBizUserInfo: function () {
    var that = this
    app.getData({
      url: app.api.getBizUserInfo(),
      data: {
        openid: wx.getStorageSync('openId')
      },
      ok: function (res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            wx.setStorageSync('bizUserInfo', res.data.data)
          }
        }
      }
    })
  },
  wxPay: function(order) {
    var openId = wx.getStorageSync("openId")
    app.getData({
      url: app.api.toPay(order, openId, 3), //仅为示例，并非真实的接口地址
      method: 'GET',
      ok: function(e) {
        console.log(e)
        wx.requestPayment({
          timeStamp: JSON.stringify((JSON.parse(e.data.params)).timeStamp),
          nonceStr: (JSON.parse(e.data.params)).nonceStr,
          package: (JSON.parse(e.data.params)).package,
          signType: 'MD5',
          paySign: (JSON.parse(e.data.params)).paySign,
          success: function(res) {
            wx.navigateTo({
              url: '../../../../mine/pages/wdck/wdck'
            })
          },
          fail: function(res) {
            console.log(e)
            wx.showToast({
              title: '支付失败',
              duration: 2000
            })
          },
          complete: function(res) {}
        })
      }
    })
  }
})